/**********************************************************************/
/*
   Author: Michelle Han
   Created: March 2023
   Last Updated: July 2025
   Description: Combine cleaned 2021 and 2022 survey data for the control group that was surveyed twice.
   Outputs:
   072125_survey_21_22_deid_clean_control_merged
   071323_survey_21_22_deid_clean_control_unmerged
   
*/
/**********************************************************************/

* include filepaths 
  if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"
 
* Log 
  cap log close
  global prefix: display %tdCYND td(`c(current_date)')
  log using "$KP_logs/${prefix}_combine_control_group_21_22.txt", text replace

* Define filepaths 
  global survey_21_clean "$KP_deid_survey/2021/Clean/071323_survey_21_deid_clean.dta"
  global survey_22_clean "$KP_deid_survey/2022/Clean/071323_survey_22_deid_clean_control_merged.dta"
  global unmerged_clean "$KP_deid_survey/2022/Clean/071323_survey_21_22_deid_clean_control_unmerged.dta"
  global merged_clean "$KP_deid_survey/2022/Clean/072125_survey_21_22_deid_clean_control_merged.dta"

  * Set variable list 
  * Only including variables both present in 2021 and 2022
  * Refer to codebook for more details 

  global varlist consented verified completed employed ///
                temp_no_work ///
                self_emp ///
                bus_owner ///
                perm_emp ///
                temp_emp ///
                fam_unpaid ///
                self_emp_bus_owner ///
                jobhours_adj ///
                multiple_jobs ///
                job_web ///
                job_satisfy_n ///
                job_search ///
                prep_new_bus ///
                hh_new_bus ///
                tot_cons_adj ///
		            tot_cons_adj_amed ///
                food_cons_adj ///
                nfood_cons_adj ///
                tot_cons_adj_l ///
                food_cons_adj_l ///
                nfood_cons_adj_l ///
                income_adj ///
                income_positive ///
                wage_adj ///
                wage_positive ///
                ladder_now ///
                ladder_5years ///
                eatless_n ///
                consmooth_savings ///
                consmooth_loans ///
                consmooth_sell ///
                consmooth_paidless ///
                consmooth_freelance ///
                consmooth_reduce ///
                consmooth_gov_aid ///
                consmooth_BLT_UMKM ///
                consmooth_move ///
                take_courses ///
                take_kp_courses ///
                num_courses /// 
                course_cert /// 
                course_cert_use /// 
                course_pay_kp /// 
                course_online /// 
                course_online_live /// 
                course_offline ///
                ewallet_own ///
                ewallet_buy ///
                ewallet_buy_online ///
                ewallet_buy_offline ///
                ewallet_bill_any ///
                ewallet_bill_mtg ///
                ewallet_bill_vphone ///
                ewallet_bill_vagent ///
                ewallet_bill_vbank ///
                ewallet_top_bank ///
                ewallet_top_agent ///
                ewallet_top_app ///
                ewallet_one_trans ///
                ewallet_multi_trans ///
                ewallet_ft_any ///
                ewallet_ft_save ///
                ewallet_ft_goods ///
                ewallet_ft_send ///
                ewallet_ft_get ///
                ewallet_ft_wage ///
                ewallet_ft_deposit ///
                ewallet_ft_wdraw ///
                bank_own ///
                bank_buy ///
                bank_buy_online ///
                bank_buy_offline ///
                bank_bill_any ///
                bank_bill_mtg ///
                bank_bill_vphone ///
                bank_bill_vagent ///
                bank_bill_vbank ///
                bank_one_trans ///
                bank_multi_trans ///
                bank_ft_any ///
                bank_ft_save ///
                bank_ft_goods ///
                bank_ft_send ///
                bank_ft_get ///
                bank_ft_wage ///
                bank_ft_deposit ///
                bank_ft_wdraw ///
                eb_own ///
                eb_buy ///
                eb_buy_online /// 
                eb_buy_offline ///
                eb_bill_any ///
                eb_bill_mtg ///
                eb_bill_vphone ///
                eb_bill_vagent ///
                eb_bill_vbank ///
                eb_one_trans ///
                eb_multi_trans ///
                eb_ft_any ///
                eb_ft_save ///
                eb_ft_goods ///
                eb_ft_send ///
                eb_ft_get ///
                eb_ft_wage ///
                eb_ft_deposit ///
                eb_ft_wdraw ///
                depressed ///
                depress_disint_n ///
                efficacy_n ///
                disinterested ///
                accomplish_goals ///
                deal_uncertainty ///
                solve_problems ///
                business_ability /// 
                views_why_poor ///
                views_gov_help_n ///
                views_allocation_n ///
                any_buy ///
                bike_buy ///
		            bike_buy_val ///
                devices_buy ///
		            devices_buy_val ///
                gold_buy ///
		            gold_buy_val ///
                livestock_buy /// 
		            livestock_buy_val ///
                any_sell ///
                bike_sell ///
		            bike_sell_val ///
                devices_sell ///
		            devices_sell_val ///
                gold_sell ///
		            gold_sell_val ///
                livestock_sell ///
		            livestock_sell_val ///
                any_lend ///
                bike_lend ///
                devices_lend ///
                gold_lend ///
                livestock_lend ///
                loan_borrow ///
                loan_get ///
                loan_lend ///
                loan_give ///
                loan_from_family /// 	
                loan_from_friends ///	
                loan_from_banks /// 		
                loan_from_microfin ///	
                loan_from_moneylend ///
                ewalletvsphone_1 ///
                ewalletvsphone_2 ///
                ewalletvsphone_3 ///
                ewalletvsphone_4 ///
                ewalletvsphone_5 /// 	
                ewalletvsphone_6 ///	
                ewalletvsphone_7 /// 		
                ewalletvsphone_8 ///	
                ewalletvsphone_9 ///
                course_hours ///
                choose_ewallet ///
                ewallet_bank_secure 



	* Load 2021 data 
  preserve  
    use "${survey_21_clean}", clear 

    keep anon_id4 $varlist
    keep if completed == 1

    * Rename variables to avoid overlap
    foreach var in $varlist {
      rename `var' `var'_21
    }

    isid anon_id4 

    tempfile survey_21_clean 
    save `survey_21_clean'
  restore

  * Merge 2022 and 2021 data for the control group
    use "${survey_22_clean}", clear

    tab control_sample
    unique anon_id4 if completed == 1 & verified == 1 & consented == 1 

    cap drop _merge
    merge m:1 anon_id4 using `survey_21_clean'

    preserve 
      keep if _merge ==1
      save "${unmerged_clean}", replace
    restore 

    keep if _merge == 3

  * Check missing values 
    mdesc 
    unique anon_id4 if control_sample==1 & completed == 1 & consented == 1 & verified == 1 & consented_21==1 & verified_21==1 & completed_21==1
  
  * Save data
    compress 
    *save "${merged_clean}" , replace
    * Save a new datset with asset value vars
    datasignature 
	    if "`r(datasignature)'" == "46540:711(26415):3359627564:3460991497" {
	   	 save "${KP_deid_survey}/2022/Clean/072125_survey_21_22_deid_clean_control_merged.dta", replace
      }
	 	else {
	     di as err "Careful, your machine produces a different dataset"
	    stop
			}
    



// DONE



	
